//
//  UIViewController+Extension.swift
//  KPBaseProject
//
//  Created by Kipp Dev on 2018/4/6.
//  Copyright © 2018年 Kipp. All rights reserved.
//

// swiftlint:disable private_over_fileprivate

import Foundation


fileprivate let shadowImage = UIImage.init(color: UIColor.lineColor, size: 1.size)
fileprivate let emptyImage = UIImage()
extension UIViewController {


    /// 设置导航栏颜色
    ///
    /// - Parameters:
    ///   - color: 导航栏背景色
    ///   - tintColor: 字体颜色
    ///   - shadowColor: 导航栏底部线颜色
    func kp_setNavigationBarColor(color: UIColor, tintColor: UIColor,shadowColor: UIColor? = UIColor.lineColor) {
        navigationController?.navigationBar.barTintColor = color
        navigationController?.navigationBar.setBackgroundImage(UIImage.init(color: color, size: 1.size), for: .default)
        navigationController?.navigationBar.tintColor = tintColor

        if (color == nil)  || (color == .clear)  {
            navigationController?.navigationBar.shadowImage = emptyImage
        }else {
            navigationController?.navigationBar.shadowImage = UIImage.init(color: shadowColor!, size: 1.size)
        }
        navigationController?.navigationBar.setTitleFont(17.font, color: tintColor)
    }


    /// 需要做滑动改变导航栏颜色
    ///
    /// - Parameters:
    ///   - color: 导航栏背景色
    ///   - tintColor: 字体颜色
    func kp_setNavigationBarScrollColor(color: UIColor, tintColor: UIColor) {
        navigationController?.navigationBar.lt_setBackgroundColor(backgroundColor: color)
        navigationController?.navigationBar.setTitleFont(17.font, color: tintColor)
        navigationController?.navigationBar.tintColor = tintColor

    }

}

extension UINavigationBar {
    private struct AssociatedKeys {
        static var overlayKey = "overlayKey"
    }

    var overlay: UIView? {
        get {
            return objc_getAssociatedObject(self, &AssociatedKeys.overlayKey) as? UIView
        }
        set {
            objc_setAssociatedObject(self, &AssociatedKeys.overlayKey, newValue as UIView?, objc_AssociationPolicy.OBJC_ASSOCIATION_RETAIN_NONATOMIC
            )
        }
    }
}

extension UINavigationBar {

    func lt_setBackgroundColor(backgroundColor: UIColor) {
        if overlay == nil {
            self.setBackgroundImage(UIImage(), for: UIBarMetrics.default)
            overlay = UIView.init(frame: CGRect.init(x: 0, y: 0, width: bounds.width, height: kNavibarH))
            overlay?.isUserInteractionEnabled = false
            overlay?.autoresizingMask = UIViewAutoresizing.flexibleWidth
            subviews.first?.insertSubview(overlay!, at: 0)
        }
        overlay?.backgroundColor = backgroundColor
    }

    func lt_setTranslationY(translationY: CGFloat) {
        transform = CGAffineTransform.init(translationX: 0, y: translationY)
    }

    func lt_setElementsAlpha(alpha: CGFloat) {
        for element in subviews {
            if element.isKind(of: NSClassFromString("UINavigationItemView") as! UIView.Type) ||
                element.isKind(of: NSClassFromString("UINavigationButton") as! UIButton.Type) ||
                element.isKind(of: NSClassFromString("UINavBarPrompt") as! UIView.Type) {
                element.alpha = alpha
            }

            if element.isKind(of: NSClassFromString("_UINavigationBarBackIndicatorView") as! UIView.Type) {
                element.alpha = element.alpha == 0 ? 0 : alpha
            }
        }

        items?.forEach({ (item) in
            if let titleView = item.titleView {
                titleView.alpha = alpha
            }
            for BBItems in [item.leftBarButtonItems, item.rightBarButtonItems] {
                BBItems?.forEach({ (barButtonItem) in
                    if let customView = barButtonItem.customView {
                        customView.alpha = alpha
                    }
                })
            }
        })
    }

    func lt_reset() {
        setBackgroundImage(nil, for: UIBarMetrics.default)
        overlay?.removeFromSuperview()
        overlay = nil
    }
}
